package com.hunan.yllxy.controller;

import com.hunan.yllxy.exceptions.CustomerException;
import com.hunan.yllxy.repostitory.dao.UserInfoDao;
import com.hunan.yllxy.repostitory.dao.UserRoleDao;
import com.hunan.yllxy.repostitory.entity.UserInfo;
import com.hunan.yllxy.repostitory.entity.UserRole;
import com.hunan.yllxy.util.MinioUtil;
import com.hunan.yllxy.util.QiniuUtil;
import io.minio.MinioClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

/**
 * @Author: 刘华
 * @Date: 2025/6/16 下午9:03
 * @Version 1.0
 */
@Slf4j
@RestController
@Api(tags = "对外开放接口", value = "对外开放接口")
@RequestMapping("/api/opening")
public class OpeningUpController {

    public static final String YLLXY = "yllxy";
    @Autowired
    private MinioUtil minioUtil;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private PasswordEncoder passwordEncoder;


    @ApiOperation("上传文件，返回文件名,不超过300MB")
    @PostMapping("upload")
    public ResponseEntity upload(MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String extension = "";
        if (originalFilename != null && originalFilename.lastIndexOf(".") != -1) {
            extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        }
        extension = extension.toLowerCase();
        String uuid = UUID.randomUUID().toString();
        long size = file.getSize();
        double fileSizeInMB = size / (1024.0 * 1024.0);
        if (fileSizeInMB>300){
            throw new CustomerException("文件过大！(请上传100MB以内)");
        }
        uuid = uuid.replaceAll("-", "");
        minioUtil.putObject(YLLXY, file, uuid + "." + extension);
        return ResponseEntity.ok(uuid + "." + extension);
    }

    @PostMapping("upload2")
    public ResponseEntity upload2(String bucketName,MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String extension = "";
        if (originalFilename != null && originalFilename.lastIndexOf(".") != -1) {
            extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        }
        extension = extension.toLowerCase();
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replaceAll("-", "");
        minioUtil.putObject(bucketName, file, uuid + "." + extension);
        return ResponseEntity.ok(uuid + "." + extension);
    }

    @ApiOperation("根据文件名下载")
    @GetMapping( value = "download", produces = "application/octet-stream")
    public void download(String bucketName, String filename, HttpServletResponse response, HttpServletRequest request) {
//        minioUtil.getObject(YLLXY, filename, response);
        minioUtil.getObject(bucketName, filename, response,request);
    }

    @ApiOperation("注册用户")
    @PostMapping( value = "registerUser")
    public void registerUser(@RequestBody UserInfo userInfo) {
        userInfo.check();
        String account = userInfo.getAccount();
        UserInfo byAccount = userInfoDao.findByAccount(account);
        if (byAccount!=null){
            throw new CustomerException("用户已存在");
        }
        String encode = passwordEncoder.encode("123456");
        userInfo.setPassword(encode);
        userInfoDao.save(userInfo);
        UserRole userRole = new UserRole();
        userRole.setRoleId("1");
        userRole.setUserId(userInfo.getId());
        userRoleDao.save(userRole);
    }


    @Autowired
    private QiniuUtil qiniuUtil;

    @PostMapping("/upload22")
    @ApiOperation("文件上传")
    public ResponseEntity upload22(MultipartFile file){
        log.info("文件上传：{}",file);
        String filePath=null;
        try {
            //文件的请求路径
             filePath = qiniuUtil.upload(file);
        } catch (Exception e) {
            log.error("文件上传失败：{}", e);
        }

        return ResponseEntity.ok(filePath);
    }


}
